package cn.itcast.chapter05.session.example02;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * @Author: 18148
 * @Date: 2020-06-05 16:23:23
 */
@WebServlet("/indexServlet")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/html;charset=utf-8");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if(user==null){
            resp.getWriter().print("您还没有登录，请<a href='/web/login.html'>登录</a>");
        }else {
            resp.getWriter().print("您已登录，欢迎你,"+user.getUsername()+"!");
            resp.getWriter().print("<a href='/web/logoutServlet'>退出</a>");
            Cookie cookie = new Cookie("JSESSIONID", session.getId());
            cookie.setMaxAge(60*30);
            cookie.setPath("/web");
            resp.addCookie(cookie);
        }
    }
}
